﻿using ManaMaple.Auth.Interface;
using ManaMaple.Entity.Sys;
using ManaMaple.IService.Sys;
using Microsoft.AspNetCore.Authorization;
using ManaMaple.Kit.Helpers;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using ManaMaple.Api.Controllers;
using ManaMaple.Models.User.Request;
using ManaMaple.Kit.Models;
using ManaMaple.Models.User.VM;

namespace ManaMaple.Api.Areas.Sys
{
    [Route("api/Sys/[controller]")]
    public class UserController : BaseController
    {
        private readonly ISysUsersService _sysUsersService;

        public UserController(IConfiguration configuration,IAuth auth, ISysUsersService sysUsersService) : base(configuration,auth)
        {
            _sysUsersService = sysUsersService;
        }

        #region 查询
        /// <summary>
        /// 列表查询
        /// </summary>
        /// <returns></returns>
        [HttpPost, Route("QueryList"), AllowAnonymous]
        public async Task<IActionResult> QueryList(QueryUserRequest query)
        {
            OperateResult result = new OperateResult();
            if (query == null) 
                result.GetFail("查询参数为NULL");
            else
                result = await _sysUsersService.QuerytPageList(query);
            return new JsonResult(result.ToRequest());
        }

        /// <summary>
        /// 用户保存
        /// </summary>
        /// <returns></returns>
        [HttpPost, Route("SaveUser"), AllowAnonymous]
        public async Task<IActionResult> SaveUser(SysUserVM vm)
        {
            OperateResult result = new OperateResult();
            if (vm == null)
                result.GetFail("保存数据为NULL");
            else
                result = await _sysUsersService.SaveUser(vm);
            return new JsonResult(result.ToRequest());
        }
        /// <summary>
        /// 删除用户
        /// </summary>
        /// <returns></returns>
        [HttpPost, Route("DelUser"), AllowAnonymous]
        public async Task<IActionResult> DelUser(OperateUserRequest dto)
        {
            OperateResult result = new OperateResult();
            if (dto == null)
                result.GetFail("查询参数为NULL");
            else
                result = await _sysUsersService.DelUser(dto);
            return new JsonResult(result.ToRequest());
        }


        /// <summary>
        /// 禁用用户
        /// </summary>
        /// <returns></returns>
        [HttpPost, Route("EnableUser"), AllowAnonymous]
        public async Task<IActionResult> EnableUser(OperateUserRequest dto)
        {
            OperateResult result = new OperateResult();
            if (dto == null)
                result.GetFail("查询参数为NULL");
            else
                result = await _sysUsersService.EnableUser(dto);
            return new JsonResult(result.ToRequest());
        }
        #endregion

    }
}
